package com.baihan.angus.app.game.busshow.ui.home.follow;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.FollowAdp;
import com.baihan.angus.app.game.busshow.databinding.FragmentFollowBinding;
import com.baihan.angus.app.game.common.bean.response.BbsResponse;
import com.baihan.angus.app.game.common.util.media.IMediaInfo;
import com.baihan.angus.app.game.common.util.media.MediaGalleryActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关注
 */
public class FollowFragment extends BaseFragment<FragmentFollowBinding, FollowVM> {

    private FollowAdp mFollowAdp;
    private Map<String, Object> mParameter = new HashMap<>();

    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;

    public FollowFragment() {
        // Required empty public constructor
    }

    public static FollowFragment newInstance() {
        FollowFragment fragment = new FollowFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.followVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_follow;
    }

    @Override
    protected void initView() {

        mFollowAdp = new FollowAdp();
        mVDBinding.rvFollow.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mFollowAdp.bindToRecycleView(mVDBinding.rvFollow);

        mVDBinding.srlFollow.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mParameter.put("page", mPage);
            mViewModel.getBbsList(mParameter);
        });

        mVDBinding.srlFollow.setOnLoadMoreListener(refreshLayout -> {
            mParameter.put("page", ++mPage);
            mViewModel.getBbsList(mParameter);
        });

        clickEvent();
    }

    @Override
    protected void initData() {
        mParameter.put("page", mPage);
        mParameter.put("size", mSize);
        //暂时调用推荐页数据，后期放开
//        mParameter.put("source", "follow");
        mViewModel.getBbsList(mParameter);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldBbs.observe(this, new Observer<BbsResponse.DataX>() {
            @Override
            public void onChanged(BbsResponse.DataX dataX) {
                if (mVDBinding.srlFollow.isRefreshing()) mVDBinding.srlFollow.finishRefresh();
                if (mVDBinding.srlFollow.isLoading()) mVDBinding.srlFollow.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mFollowAdp.setNewData(dataX.getData());
                } else {
                    mFollowAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlFollow.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlFollow.setEnableLoadMore(false);
                }
            }
        });
    }

    private void clickEvent() {
        mFollowAdp.setOnMediaAdpCallback(new FollowAdp.OnMediaAdpCallback() {
            @Override
            public void preview(int index, List<IMediaInfo> list) {
                MediaGalleryActivity.start(getActivity(), list, index, 0);
            }
        });

        mFollowAdp.setOnItemClickListener(new FollowAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }

            @Override
            public void onLike(int position) {

            }

            @Override
            public void onShare(int position) {

            }

            @Override
            public void onCollect(int position) {

            }

            @Override
            public void onComment(int position) {

            }
        });
    }

    @Override
    protected void onVisibleAgain() {
        mVDBinding.srlFollow.autoRefresh();
    }
}